package cn.ava.basic.service.impl;

import cn.ava.basic.mapper.BaseMapper;
import cn.ava.basic.query.BaseQuery;
import cn.ava.basic.service.IBaseService;
import cn.ava.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * @ClassName: BaseServiceImpl
 * @Description: 抽取公共的service实现类
 * @Author: Avavaava
 * @Date: 2022/6/27 17:41
 * @version: 23.53
 *
 */
@Service
// 开启事务 只读属性和支持事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * @Title: loadAll
     * @Description: 查询全部数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:46
     * @param:
     * @return:
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * @Title: getById
     * @Description: 根据Id查找数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:47
     * @param:
     * @return:
     */
    @Override
    public T getById(Long id) {
        return baseMapper.getById(id);
    }

    /**
     * @Title: delete
     * @Description: 根据id删除数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:47
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    /**
     * @Title: add
     * @Description: 添加数据方法
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:47
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);

    }

    /**
     * @Title: update
     * @Description: 修改数据方法
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:48
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void update(T t) {
        
        baseMapper.update(t);
    }


    /**
     * @Title: queryData
     * @Description: 高级查询
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:48
     * @param:
     * @return:
     */
    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        Integer total = baseMapper.queryCount(baseQuery);
        List<T> rows = baseMapper.queryData(baseQuery);
        return new PageList<>(total,rows);
    }

    /**
     * @Title: batchDelete
     * @Description: 批量删除方法
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/6/27 17:48
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void batchDelete(Long[] id) {
        baseMapper.batchDelete(id);
    }


}
